﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Element.Net.Dialogs
{
    public enum AlertType
    {
        Info,
        Warn,
        Success,
        Error
    }

    public partial class AlertDialog : Form
    {
        private readonly Color successForegroundColor = ColorTranslator.FromHtml("#67c23a");
        private readonly Color successBackgroundColor = ColorTranslator.FromHtml("#f0f9eb");

        private readonly Color errorForegroundColor = ColorTranslator.FromHtml("#f56c6c");
        private readonly Color errorBackgroundColor = ColorTranslator.FromHtml("#fef0f0");

        private readonly Color infoForegroundColor = ColorTranslator.FromHtml("#909399");
        private readonly Color infoBackgroundColor = ColorTranslator.FromHtml("#f4f4f5");

        private readonly Color warnForegroundColor = ColorTranslator.FromHtml("#e6a23c");
        private readonly Color warnBackgroundColor = ColorTranslator.FromHtml("#fdf6ec");
        
        private static int count = 1;

        public AlertDialog(string message = "", AlertType alertType = AlertType.Info, int interval = 5000)
        {
            InitializeComponent();
            InitDialog(alertType);
            InitMessage(message);
            InitInterval(interval);
        }

        private void InitDialog(AlertType alertType)
        {
            ShowInTaskbar = false;
            switch (alertType)
            {
                case AlertType.Info:
                    SetAlertType(infoForegroundColor, infoBackgroundColor, Properties.Resources.info);
                    break;
                case AlertType.Error:
                    SetAlertType(errorForegroundColor, errorBackgroundColor, Properties.Resources.error);
                    break;
                case AlertType.Success:
                    SetAlertType(successForegroundColor, successBackgroundColor, Properties.Resources.success);
                    break;
                case AlertType.Warn:
                    SetAlertType(warnForegroundColor, warnBackgroundColor, Properties.Resources.warn);
                    break;
            }
        }

        private void SetAlertType(Color foreground, Color background, Image image)
        {
            label1.ForeColor = foreground;
            label1.BackColor = background;
            
            pictureBox1.BackColor = background;
            pictureBox2.BackColor = background;

            elementZone1.BackColor = background;
            pictureBox2.Image = image;
        }

        private void InitInterval(int interval)
        {
            new Task(() =>
            {
                Thread.Sleep(interval);
                if (InvokeRequired)
                {
                    Invoke(new Action(() =>
                    {
                        Close();
                        Dispose();
                    }));
                }
            }).Start();
        }

        private void InitMessage(string message)
        {
            int x = (SystemInformation.WorkingArea.Width - Size.Width) / 2;
            int y = (Size.Height + 10) * count++;
            StartPosition = FormStartPosition.Manual;
            Location = new Point(x, y);
            Point point = new Point(40, Height / 2 - label1.Height / 2);
            label1.Location = point;
            label1.Text = message;
        }

        /// <summary>
        /// 绘制圆角路径
        /// </summary>
        private GraphicsPath GetGraphicsPath(Rectangle rect, float radius)
        {
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
            graphicsPath.AddArc(rect.Width - radius, rect.Y, radius, radius, 270, 90);
            graphicsPath.AddArc(rect.Width - radius, rect.Height - radius, radius, radius, 0, 90);
            graphicsPath.AddArc(rect.X, rect.Height - radius, radius, radius, 90, 90);
            graphicsPath.CloseAllFigures();
            return graphicsPath;
        }

        /// <summary>
        /// 高质量绘制矩形
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="rectangle"></param>
        /// <param name="color"></param>
        private void DrawRectangle(Graphics graphics, Rectangle rectangle, Color color, float radius)
        {
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
            GraphicsPath graphicsPath = GetGraphicsPath(rectangle, radius);
            SolidBrush solidBrush = new SolidBrush(color);
            graphics.FillPath(solidBrush, graphicsPath);
        }

        /// <summary>
        /// 重绘按钮
        /// </summary>
        /// <param name="pevent"></param>
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(0, 0, Width, Height);
            DrawRectangle(graphics, rectangle, BackColor, 15);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            //base.OnPaintBackground(pevent);
            Graphics graphics = pevent.Graphics;
            Rectangle rectangle = new Rectangle(0, 0, Width, Height);
            DrawRectangle(graphics, rectangle, Color.Transparent, 1f);
        }
    }
}
